package com.kgc.mall.servlet;

import cn.hutool.core.util.StrUtil;
import com.kgc.mall.common.Constants;
import com.kgc.mall.pojo.User;
import com.kgc.mall.service.UserService;
import com.kgc.mall.service.UserServiceImpl;
import com.kgc.mall.util.MyBatisUtil;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = {"/backend/users/*"})
public class UserInfoServlet extends BaseServlet {
  private static UserService userService = new UserServiceImpl();
  protected  void  listPage(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
  //前台传数据给后台
  String curPage = request.getParameter("page");
  String userName = request.getParameter("userName");
  String loginName = request.getParameter("loginName");
  int pageNum = 1;
  if(StrUtil.isNotBlank(curPage)){
    pageNum = Integer.parseInt(curPage);
  }
  int pageCount = (int) userService.getCount()+1;
  List<User> userList =   userService.getUserInfoByPage(loginName,userName,pageNum,Constants.PAGE_SIZE);
  //后台传数据给前台
    request.setAttribute("pageNum",pageNum);
    request.setAttribute("pageCount",pageCount);
  request.setAttribute("userList",userList);
  RequestDispatcher dispatcher = request.getRequestDispatcher("/userList.jsp");
  dispatcher.forward(request,response);
}

  protected  void  search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //前台传数据给后台
    String curPage = request.getParameter("page");
    String userName = request.getParameter("userName");
    String loginName = request.getParameter("loginName");
    int pageNum = 1;
    if(StrUtil.isNotBlank(curPage)){
      pageNum = Integer.parseInt(curPage);
    }
    List<User> search =   userService.getUser(loginName,userName);
    //后台传数据给前台
    request.setAttribute("userList",search);
    RequestDispatcher dispatcher = request.getRequestDispatcher("/backend/users?action=listPage");
    dispatcher.forward(request,response);
  }

//尾页
  protected  void  lastPage(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
    //前台传数据给后台
    String userName = request.getParameter("userName");
    String loginName = request.getParameter("loginName");
    int pageNum = (int)userService.getCount()+1;
    List<User> userList =   userService.getUserInfoByPage(loginName,userName,pageNum,Constants.PAGE_SIZE);
    //后台传数据给前台
    request.setAttribute("userList",userList);
    RequestDispatcher dispatcher = request.getRequestDispatcher("/backend/users?action=listPage");
    dispatcher.forward(request,response);
  }
//删除
  protected  void  deleteUser(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
    //前台传数据给后台
    String userId = request.getParameter("userId");
    userService.deleteUserByUid(Integer.parseInt(userId));
    MyBatisUtil.commit();
    RequestDispatcher dispatcher = request.getRequestDispatcher("/backend/users?action=listPage");
    dispatcher.forward(request,response);
  }

}
